/*
AJAX feito para método POST.
Adaptado por Micael Vianna (micael [at] aiatola [dot] net
Código original:
	http://www.captain.at/howto-ajax-form-post-get.php
*/

function createXMLHttpRequest() {
	/*
		Interface genérica para acesso ao conector remoto.
	*/
   try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){}
   try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
   try{ return new XMLHttpRequest(); }catch(e){}
   alert("XMLHttpRequest not supported");
   return null;
}
	// contem objeto para solicitar recursos remotos.
var xhReq;
// Envia formulario inteiro via POST
// Trata campos sem a necessidade de verificação.
function xhSend(url,formu,funcao){
	/*
		envia a requisicao para o servidor, e tudo o que eh
		retornado eh passado a funcao do_readyStateChange
	*/
	xhReq = createXMLHttpRequest();
	var form = document.getElementById(formu);
	var form_string = get(form);
	var funcao;
	xhReq.open("post",url,true);
	xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	//xhReq.setRequestHeader('Content-Lenght',form_string.lenght);
	
    xhReq.onreadystatechange = funcao;
    xhReq.send(form_string);
}
/*
EXEMPLO DE FUNÇÃO
function do_readyStateChange(){
	/*
		se a requisicao estiver completa entao exibir o
		valor no campo de formulario com id total.
	*/
    /*
	if(xhReq.readyState!=4){return;}
	var total=document.getElementById(DIVDEDESTINO);
    total.innerHTML=xhReq.responseText;
}*/


function get(obj) {
  var getstr = "";
  for (i=0; i<obj.length; i++) {

		if (obj.elements[i].tagName == "INPUT") {
		if (obj.elements[i].type == "text" || obj.elements[i].type == "hidden" || obj.elements[i].type == "password" ) {
		   getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
		}
		if (obj.elements[i].type == "checkbox") {
		   if (obj.elements[i].checked) {
			  getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
		   } else {
			  getstr += obj.elements[i].name + "=&";
		   }
		}
		if (obj.elements[i].type == "radio") {
		   if (obj.elements[i].checked) {
			  getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
		   }
		}
	 }   
	 if (obj.elements[i].tagName == "SELECT") {
		var sel = obj.elements[i];
		getstr += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
	 }
 	if (obj.elements[i].type == "textarea") {
	   getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
	}
  }
  return getstr;
}

// url de destino, parametros em formato get, funcao que vai executar

function include(url,parametros,div){
/*
		envia a requisicao para o servidor, e tudo o que eh
		retornado eh passado a funcao do_readyStateChange
		usar parametros no formato: variavel=texto&variavel2=texto2
	*/
//	var form = document.getElementById(formu);
//	var form_string = get(form);
//	var funcao;
	xhReq = createXMLHttpRequest();
	xhReq.open("post",url,true);
	xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	//xhReq.setRequestHeader('Content-Lenght',form_string.lenght);
	
    xhReq.onreadystatechange = function() {
		if(xhReq.readyState==4){ 
			document.getElementById(div).innerHTML = xhReq.responseText; 
		}
		if(xhReq.readyState!=4){ 
			return;
		}
	}
	xhReq.send(parametros);
}

function executar(url,parametros,funcao){
/*
		envia a requisicao para o servidor, e tudo o que eh
		retornado eh passado a funcao do_readyStateChange
		usar parametros no formato: variavel=texto&variavel2=texto2
	*/
//	var form = document.getElementById(formu);
//	var form_string = get(form);
//	var funcao;
	xhReq = createXMLHttpRequest();
	xhReq.open("post",url,true);
	xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	//xhReq.setRequestHeader('Content-Lenght',form_string.lenght);
	
    xhReq.onreadystatechange = funcao;
	xhReq.send(parametros);
}

//
//	exemplo de funlçao.
function adiciona() {
	if(xhReq.readyState==4){ 
		document.getElementById('centro').innerHTML = xhReq.responseText; 
	}
	if(xhReq.readyState!=4){ 
		return;
	}
}